package com.itzb.asone.system.service.impl;


import java.util.List;

import com.itzb.asone.constant.model.SysUserConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysUser;
import com.itzb.asone.system.repository.ISysUserRepository;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysUserService;

public class SysUserServiceImpl implements ISysUserService{
	
	private ISysUserRepository sysUserRepository;
	private IMessageService messageService;

	public List<SysUser> queryPage(Criteria<SysUser> param){
		return sysUserRepository.queryPage(param);
	}

	public Integer queryPageCount(Criteria<SysUser> param){
		return sysUserRepository.queryPageCount(param);
	}

	public SysUser queryEntityById(String id){
		return sysUserRepository.queryEntityById(id);
	}
	
	public SysUser queryEntityByUserId(String userId){
		Criteria<SysUser> param = new Criteria<SysUser>();
		param.addParam("userId", userId);
		List<SysUser> list = sysUserRepository.queryPage(param);
		SysUser sysUser = new SysUser();
		if(list == null || list.size() == 0){
			return null;
		} else {
			sysUser = list.get(0);
		}
		return sysUser;
	}
	
	public SysUser queryEntityByUserName(String userName){
		Criteria<SysUser> param = new Criteria<SysUser>();
		param.addParam("userName", userName);
		List<SysUser> list = sysUserRepository.queryPage(param);
		SysUser sysUser = new SysUser();
		if(list == null || list.size() == 0){
			return null;
		} else {
			sysUser = list.get(0);
		}
		return sysUser;
	}
	
	public void addPasswordRepTimes(String userId) {
		Criteria<SysUser> param = new Criteria<SysUser>();
		param.addParam("id", userId);
		List<SysUser> list = sysUserRepository.queryPage(param);
		SysUser sysUser = new SysUser();
		if(list == null || list.size() == 0){
			return;
		} else {
			sysUser = list.get(0);
			int errorNum = sysUser.getPasswordRepTimes() + 1;
			if(errorNum >= 5){
				sysUser.setStatus(SysUserConstant.T_SYS_USER_STATUS_3);
			}
			sysUser.setPasswordRepTimes(errorNum);
			param.setRecord(sysUser);
			this.updateByCriteria(param);
		}
	}
	
	public List<SysUser> queryUserInfo(Criteria<SysUser> param){
		return sysUserRepository.queryUserInfo(param);
	}

	public Integer insert(SysUser record){
		return sysUserRepository.insert(record);
	}

	public Integer updateByCriteria(Criteria<SysUser> param){
		return sysUserRepository.updateByCriteria(param);
	}

	public Integer deleteById(String id){
		return sysUserRepository.deleteById(id);
	}

	public Integer deleteByCriteria(Criteria<SysUser> param){
		return sysUserRepository.deleteByCriteria(param);
	}

	public ISysUserRepository getSysUserRepository() {
		return sysUserRepository;
	}

	public void setSysUserRepository(ISysUserRepository sysUserRepository) {
		this.sysUserRepository = sysUserRepository;
	}

	public IMessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}

}
